package quant.smart.crawl.utils;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class TimeUtil {
	
	/**
	 * 年月日
	 */
	public static final String YYYY_MM_DD = "yyyy-MM-dd";
	public static final String YYYYMMDD = "yyyyMMdd";
	
	/**
	 * 时分秒
	 */
	public static final String HH_MM_SS = "HH:mm:ss";
	public static final String HHMMSS = "HHmmss";
	
	/**
	 * 年月日时分秒
	 */
	public static final String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
	public static final String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";	
	/**
	 * 年月日时分秒毫秒
	 */
	public static final String YYYY_MM_DD_HH_MM_SS_SSS = "yyyy-MM-dd HH:mm:ss.SSS";
	public static final String YYYYMMDDHHMMSSSSS = "yyyyMMddHHmmssSSS";
	
	
	public static Calendar toCalendar(String dateStr)  {
		return toCalendar(dateStr, YYYY_MM_DD);
	}
	
	public static String toString(Calendar cal) {
		return toString(cal, YYYY_MM_DD);
	}
	
	public static Timestamp toTimestamp(String str) {
		return toTimestamp(str, YYYY_MM_DD);
	}
	
	public static String toString(Timestamp stamp) {
		return toString(stamp, YYYY_MM_DD);
	}
	
	public static String toStringHourseMinuteSecond (Timestamp stamp) {
		return toString(stamp, YYYY_MM_DD_HH_MM_SS);
	}
	public static Calendar toCalendar(String dateStr, String pattern)  {
		SimpleDateFormat format = new SimpleDateFormat(pattern);
		Calendar cal = Calendar.getInstance();
		cal.clear();
		try {
			cal.setTime(format.parse(dateStr));
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return cal;
	}
	
	public static String toString(Calendar cal, String pattern)  {
		SimpleDateFormat format = new SimpleDateFormat(pattern);
		return format.format(cal.getTime());
	}
	
	public static String toString(long ms, String pattern)  {
		SimpleDateFormat format = new SimpleDateFormat(pattern);
		return format.format(ms);
	}
	public static String toString(long ms,String pattern,int num){
		SimpleDateFormat format = new SimpleDateFormat(pattern);
		return format.format(ms+num*24*60*60*1000); 
	}
	public static String toString(Date date) {
		SimpleDateFormat format = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS);		
		return format.format(date);
	}
	
	public static String toStringSs(Date date) {
		SimpleDateFormat format = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS_SSS);		
		return format.format(date);
	}
	
	public static Date toDateSS (String dateFormatStr) throws ParseException {
		SimpleDateFormat format = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS_SSS);		
		return format.parse(dateFormatStr);
	}
	
	public static String toTime(Timestamp stamp) {
		return toString(stamp, YYYY_MM_DD_HH_MM_SS);
	}
	
	public static String toTime(Date dt) {
		return toString(dt, YYYY_MM_DD_HH_MM_SS);
	}
	
	public static String toString(Date date, String pattern) {
		Calendar cal = Calendar.getInstance();
		cal.clear();
		cal.setTime(date);
		return toString(cal, pattern);
	}
	
	public static String toString(Timestamp stamp, String pattern) {
		return toString((Date) stamp, pattern);
	}
	
	public static Timestamp toTimestamp(String str, String pattern) {
		Calendar cal = toCalendar(str, pattern);
		Timestamp stamp = new Timestamp(cal.getTimeInMillis());
		return stamp;
	}
	
	public static Calendar copy(Calendar cal) {
		Calendar c = Calendar.getInstance();
		c.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH), 
				cal.get(Calendar.HOUR_OF_DAY), cal.get(Calendar.MINUTE), cal.get(Calendar.SECOND));
		c.set(Calendar.MILLISECOND, cal.get(Calendar.MILLISECOND));
		return c;
	}
	
	/**
	 * 计算c2 - c1 之间间隔的天数
	 * @param c1 起始日期
	 * @param c2 终止日期
	 * @return
	 */
	public static double daysBetween(Calendar c1, Calendar c2) {
		long start = c1.getTimeInMillis();
		long end = c2.getTimeInMillis();
		return (end - start) / (24 * 60 * 60 * 1000d);
	}
	
	/**
	 * 计算c2 - c1 之间间隔的天数  YYYY_MM_DD 格式
	 * @param c1 起始日期
	 * @param c2 终止日期
	 * @return
	 */
	public static double daysBetween(Timestamp c1, Timestamp c2) {
		String stratStr=toString(c1,YYYY_MM_DD);
		String endStr=toString(c2,YYYY_MM_DD);
		Timestamp startstamp =toTimestamp(stratStr);
		Timestamp endtamp =toTimestamp(endStr);
		
		long start = startstamp.getTime();
		long end = endtamp.getTime();
		return (end - start) / (24 * 60 * 60 * 1000d);
	}
	
	/**
	 * 保留年月日
	 * 时分秒和毫秒置0
	 * @param cal
	 */
	public static void retainYYMMDD(Calendar cal) {
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MILLISECOND, 0);
	}
	
	/**
	 * 按给定的格式获取当前时间：字符串
	 * @param pattern
	 * @return
	 */
	public static String getCurrentTimeAsStr(String pattern){
		Calendar c = Calendar.getInstance();
		return new SimpleDateFormat(pattern).format(c.getTimeInMillis());
	}
}
